package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.example.xymarket.adapter.GoodsAdapter;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.po.Goods;
import com.example.xymarket.httpUtil.GoodsService;
import com.example.xymarket.common.MyHandler;

import java.util.List;

public class SearchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        mEdit=(EditText)findViewById(R.id.key_word_input);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                updateListView((List<Goods>) msg.obj);
            }
        }
    };

    EditText mEdit;

    public void getRes(View view)
    {
        String res=mEdit.getText().toString();
        MyHandler myHandler=new MyHandler(getApplicationContext(),handler);
        GoodsService goodsService=new GoodsService(myHandler);
        goodsService.getGoodsByKeyWord(res);
    }

    public void updateListView(List<Goods> list)
    {
        if(list==null)return;
        ListView listView = (ListView) findViewById(R.id.search_listView);
        try {
            GoodsAdapter adapter = new GoodsAdapter(list,this,R.layout.item_home,handler);
            listView.setAdapter(adapter);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }

    public void Back(View view)
    {
        finish();
    }
}